﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using SocketLib;
namespace LogSvc
{
	public partial class Form1 : Form
	{

		Dictionary<int, fileInfo> fileList = new Dictionary<int, fileInfo>();

		SocketLib.Server s = new SocketLib.Server();

		ArrayList temp = new ArrayList();

		public Form1()
		{
			InitializeComponent();
			Form.CheckForIllegalCrossThreadCalls = false;


		}

		private void button1_Click(object sender, EventArgs e)
		{

			
		}
		public void start()
		{
			s.Start();
			//客户端连接上
			s.Accept_EventHandler += new SocketLib.Server.Accept_Delegate(s_AcceptEventHandler);

			//接收到消息
			s.Callback_EventHandler += new SocketLib.Server.CallbackDelegate(s_CallbackEventHandler);

			//关闭事件
			s.Closed_EventHandler += new Server.Closed_Delegate(s_Closed_EventHandler);

		}

		//客户端关闭连接
		void s_Closed_EventHandler(object sender, Server.Closed_EventArgs e)
		{
			Console.WriteLine(e.socket.RemoteEndPoint.ToString() + "断开连接");
		}

		void s_CallbackEventHandler(object sender, SocketLib.Server.CallbackEventArgs e)
		{

			IPMPack c = new IPMPack();
			c.UnPacked(e.buffer);
            

			if (c.PackType == 2)
			{

				string str = System.Text.Encoding.Default.GetString(c.Data);
				string[] arr = str.Split(':');

				if (!fileList.ContainsKey(c.PackNo))
				{
					fileInfo fi = new fileInfo();
					fi.FileName = arr[0];
					fi.FileSize = int.Parse(arr[1]);
					fileList.Add(c.PackNo, fi);
					//Console.WriteLine("准备接收文件:" + arr[0] + "文件大小" + arr[1]);

				}

			}

			if (c.PackType == 3)
			{
				//Console.WriteLine("接收文件:" + fileList[c.PackNo].FileName + " 大小 " + c.PackSize.ToString() + "["+fileList[c.PackNo].FileDate.Length+"/" + fileList[c.PackNo].FileSize.ToString()+"]");

				if (fileList[c.PackNo].FileDate == null)
				{
					//Console.Write("0%");


					fileList[c.PackNo].FileDate = c.Data;
				}
				else
				{


					fileList[c.PackNo].FileDate = MergeBytes(fileList[c.PackNo].FileDate, c.Data);


				}

				if (fileList[c.PackNo].FileDate.Length == fileList[c.PackNo].FileSize)
				{
					Console.WriteLine("接收完毕开始合并!");
					FileStream fs = new FileStream(@"D:\" + DateTime.Now.Second.ToString() + fileList[c.PackNo].FileName, FileMode.Create, FileAccess.Write);
					fs.Write(fileList[c.PackNo].FileDate, 0, fileList[c.PackNo].FileSize);
					fs.Flush();
					fs.Close();
					fileList.Remove(c.PackNo);

				}


			}

			//DateTime dt2 = DateTime.Now;
			//Console.WriteLine("接收"+(dt2-dt1).Milliseconds.ToString());


		}

		void s_AcceptEventHandler(object sender, SocketLib.Server.Accept_EventArgs e)
		{
			Console.WriteLine(e.ip);
		}


		byte[] MergeBytes(byte[] pByteA, byte[] pByteB)
		{
			MemoryStream ms = new MemoryStream();
			ms.Write(pByteA, 0, pByteA.Length);
			ms.Write(pByteB, 0, pByteB.Length);

			//return ms.GetBuffer();
			return ms.ToArray();
		}

        public class fileInfo
        {
            public string FileName { get; set; }
            public int FileSize { get; set; }
            public byte[] FileDate { get; set; }

        }

		private void button2_Click(object sender, EventArgs e)
		{
			
		}
	}


}
